﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YGRABPRN : DocumentBase
    {
        #region-------------Properties-----------------------

        YGRABPRNData m_Data; 
        ITXEXRPT ITXEXRPT;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetDetail");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        #region=====vwMaster=======
        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if ((DataRowView)vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwARANS=======
        CollectionViewSource vwARANS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwARANS");
            }
        }
        DataRow dwARANS
        {
            get
            {
                if (vwARANS.View == null)
                    return null;
                if ((DataRowView)vwARANS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwARANS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwMU=======
        CollectionViewSource vwMU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMU");
            }
        }
        DataRow dwMU
        {
            get
            {
                if (vwMU.View == null)
                    return null;
                if ((DataRowView)vwMU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMU.View.CurrentItem).Row;
            }
        }
        #endregion
        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtAUFLFD.Text = value;
            }
        }
        string POS
        {
            get
            {
                return m_Data.Right("000" + txtAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtAUFPOS.Text = value;
            }
        }
        #endregion
        public YGRABPRN()
        {
            InitializeComponent();
            this.Name = "生产打印";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            //IsSetUp = true;
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                base.SetUp(dicParamater);
                InitializeComponent();
            }
            m_Data = new YGRABPRNData();
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";
            txtUserID.Text = app.User;
            txtPassword.Password = app.Pwd;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                m_Data = new YGRABPRNData();
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;
                if (app.User.Trim() == "ITXV")
                {
                    KRS = "B4";
                    LFD = "080002";
                    btnQuery_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            expLog.IsExpanded = false;
            btnQuery.IsEnabled = true;
        }
        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                SetDetail.Clear();
                DataRow[] rows = SetData.ABPOS.Select("ABPAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "'");
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ABPOS";
                SetDetail.Merge(tb);
                rows = SetData.ABFOLGE.Select("ABFAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "'");
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "ABFOLGE";
                    SetDetail.Merge(tb);
                }
                rows = SetData.GAAUF.Select("GAAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "'");
                if (rows.Length > 0)
                {

                    tb = rows.CopyToDataTable();
                    tb.TableName = "GAAUF";
                    SetDetail.Merge(tb);
                }

                rows = SetData.ZUTEI.Select("ZUAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "'");
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "ZUTEI";
                    SetDetail.Merge(tb);
                }
                rows = SetData.Tables["ABAUFM"].Select("ABAAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "'");
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "ABAUFM";
                    SetDetail.Merge(tb);
                }
                int max = 1;
                var qAR = from row in SetData.ARANS.AsEnumerable()
                          where row["ARAAUFPOS"].ToString() == dwMaster["ABPAUFPOS"].ToString()
                          select row;

                foreach (DataRow row in qAR)
                {
                    int ARAANSNR = Convert.ToInt16(row["ARAANSNR"].ToString());
                    if (ARAANSNR > max)
                    {
                        max = ARAANSNR;
                    }
                }
                DataRow dwARANS = null;
                rows = SetData.ARANS.Select("ARAAUFPOS='" + dwMaster["ABPAUFPOS"].ToString() + "' AND ARAANSNR='" + max + "'");
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "ARANS";
                    SetDetail.Merge(tb);
                    foreach (DataRow row in tb.Rows)
                    {
                        dwARANS = row;
                    }
                }
                if (dwARANS != null)
                {
                    rows = SetData.LRPOS.Select("LRPREZART='" + dwARANS["ARAREZART"].ToString() + "' AND LRPREZNR='" + dwARANS["ARAREZNR"].ToString() + "'");
                    if (rows.Length > 0)
                    {
                        tb = rows.CopyToDataTable();
                        tb.TableName = "LRPOS";
                        SetDetail.Merge(tb);
                    }
                }
                SetDetail.Merge(SetData.Tables["ABKOPF"]);
                SetDetail.Merge(SetData.Tables["GSDAT"]);
                SetDetail.Merge(SetData.Tables["GSCOLOR"]);
                SetDetail.Merge(SetData.Tables["GSMATK"]);
                if (vwARANS.View != null)
                {
                    vwARANS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet Result = new DataSet();
                using (new LongOperation(this, "正在生成报表，请耐心等待"))
                {
                    Result = m_Data.BuildData("00", KRS, LFD);
                }
                SetData.Clear();
                SetData.Merge(Result);
                if (vwMaster != null)
                {
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        { 
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "AB50GB12";
            ITXEXRPT.strExcelFile = app.company + @"\YGRAB50DOC.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintABDOC_Click(object sender, RoutedEventArgs e)
        { 
            ITXEXRPT.dsSource = SetDetail;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType =  "YGRAB50DOC";
            ITXEXRPT.strExcelFile = app.company + @"\YGRABDOC.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void btnPrintAR_Click(object sender, RoutedEventArgs e)
        {
            
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = app.company + "AB50AR";
            ITXEXRPT.strExcelFile = app.company + @"\YGRAB50DOC.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        private void btnPrintMU_Click(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
            {
                MessageBox.Show("请选择染批行");
                return;
            }
            string APOS = dwMU["ABAAPOS"].ToString();
            SetDetail.Tables["ABAUFM"].Clear();
            DataRow[] rows = SetData.Tables["ABAUFM"].Select("ABAAUFPOS='" + dwMU["ABAAUFPOS"].ToString() + "' AND ABAAPOS='" + APOS + "'");
            if (rows.Length > 0)
            {
               DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ABAUFM";
                SetDetail.Merge(tb);
            }
            DataRow dwData = SetData.ZUTEI.NewRow();
            dwData["ZUMANDANT"] = dwMU["ABAMANDANT"];
            dwData["ZUAUFKRS"] = dwMU["ABAAUFKRS"];
            dwData["ZUAUFLFD"] = dwMU["ABAAUFLFD"];
            DataSet SResult = INTEXDBC.QueryZUTEI(dwData, 8);
            SetDetail.ZUTEI.Clear();
            SetDetail.Merge(SResult); 

            ITXEXRPT.dsSource = SetDetail;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = "JLTGB20LABEL";
            ITXEXRPT.strExcelFile = app.company + @"\YGRABPRN.xls";
            string filename = ITXEXRPT.WriteExcelReport();
            vwMU.View.MoveCurrentTo(-1);
        }
        
        private void txtAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }
    }
}
